001 /** 002 * Created by IntelliJ IDEA. 003 * User: Wei Wang 004 * Date: Nov 27, 2002 005 * Time: 3:50:38 PM 006 */ 007 008 package EVolve.visualization.XYViz; 009 010 import EVolve.visualization.*; 011 import EVolve.util.painters.*; 012 import EVolve.util.*; 013 import EVolve.util.phasedetectors.PhaseDetector; 014 import EVolve.util.sourcebrowser.SourceBrowser; 015 import EVolve.Scene; 016 import javax.swing.*; 017 import java.util.*; 018 import java.awt.event.*; 019 import java.awt.*; 020 021 public abstract class XYVisualization extends Visualization{ 022 protected int autoInterval,interval; 023 protected long xMax, xOffset, currentThread; 024 protected String autoPredictorname; 025 protected boolean shift_pressed, flipMagnifier = true; 026 protected Painter painter; 027 protected Magnifier magnifier; 028 protected JMenuItem itemChangeOrientation,itemScale,itemBrowseSource; 029 protected boolean normalOrientation; 030 protected HashMap imageMap,threadDataSet; 031 protected SourceBrowser sourceBrowser; 032 protected AxesPanel canvas; 033 protected AutoImage image; // the image 034 protected ArrayList timeMap; 035 protected PhaseDetector phaseDetector; 036 protected boolean phaseDetectorEnabled; 037 038 protected abstract void mouseMove(int x, int y); 039 040 public XYVisualization() { 041 super(); 042 shift_pressed = false; 043 autoInterval = -1; 044 autoPredictorname = null; 045 interval = -1; 046 normalOrientation = true; 047 currentThread = -1; 048 imageMap = new HashMap(); 049 threadDataSet = new HashMap(); 050 sourceBrowser = new SourceBrowser(); 051 timeMap = new ArrayList(); 052 phaseDetectorEnabled = Scene.getVisualizationManager().isPhaseDetectorEnabled(); 053 } 054 055 protected Component createPanel() { 056 canvas = new AxesPanel(AxesPanel.Axis_Enabled | AxesPanel.Flip_Image | AxesPanel.Scale_Image); 057 flipMagnifier = true; 058 addMagnifier(canvas,flipMagnifier); 059 JScrollPane returnVal = new JScrollPane(canvas); 060 canvas.setParent(returnVal); 061 062 return returnVal; 063 } 064 065 protected void addMagnifier(Component aPanel, boolean flipImage) { 066 magnifier = new Magnifier(flipImage); 067 aPanel.addMouseMotionListener(new MouseMotionAdapter() { 068 public void mouseMoved(MouseEvent e) { 069 070 if (e.isShiftDown()) shift_pressed = true; 071 else shift_pressed = false; 072 073 if (e.isControlDown()) { 074 int x = e.getX(), y = e.getY(); 075 x = canvas.getImageX(x); 076 y = canvas.getImageY(y); 077 magnifier.convertMousePosition(canvas); 078 magnifier.showWindow(canvas.getSubImage(x,y)); 079 canvas.drawZoomingArea(e.getX(),e.getY()); 080 } 081 mouseMove(e.getX(), e.getY()); 082 } 083 084 public void mouseDragged(MouseEvent e) { 085 if (e.isShiftDown()) shift_pressed = true; 086 else shift_pressed = false; 087 mouseMove(e.getX(), e.getY()); 088 } 089 }); 090 091 092 aPanel.addMouseListener(new MouseListener() { 093 public void mouseClicked(MouseEvent e) { 094 mouseX = e.getX(); 095 mouseY = e.getY(); 096 freezed = false; 097 } 098 099 public void mouseEntered(MouseEvent e) {} 100 101 public void mouseExited(MouseEvent e) {} 102 103 public void mousePressed(MouseEvent e) {} 104 105 public void mouseReleased(MouseEvent e) {} 106 }); 107 108 } 109 110 protected void updateConfiguration() { 111 if (panel instanceof JScrollPane) 112 ((AxesPanel)(((JScrollPane)panel)).getViewport().getView()).setImage(null); 113 else 114 ((AxesPanel)panel).setImage(null); 115 panel.repaint(); 116 117 normalOrientation = true; 118 } 119 120 protected void createMenu() { 121 super.createMenu(); 122 123 itemScale = new JMenuItem("Restore"); 124 itemScale.setMnemonic(KeyEvent.VK_R); 125 itemScale.addActionListener(new ActionListener() { 126 public void actionPerformed(ActionEvent e) { 127 if (e.getActionCommand().charAt(0) == 'F') { 128 canvas.scaleImage(true); 129 itemScale.setText("Restore"); 130 itemScale.setActionCommand("Restore"); 131 itemScale.setMnemonic(KeyEvent.VK_R); 132 } else { 133 canvas.scaleImage(false); 134 itemScale.setText("Fit Window"); 135 itemScale.setActionCommand("Fit Window"); 136 itemScale.setMnemonic(KeyEvent.VK_F); 137 } 138 canvas.repaint(); 139 } 140 }); 141 itemScale.setSelected(true); 142 143 itemBrowseSource = new JMenuItem("Browse source code..."); 144 itemBrowseSource.setMnemonic(KeyEvent.VK_B); 145 itemBrowseSource.addActionListener(new ActionListener() { 146 public void actionPerformed(ActionEvent e) { 147 sourceBrowser.showSourceFile(getEntityUnderMouse()); 148 } 149 }); 150 popup.add(itemBrowseSource); 151 popup.add(itemScale); 152 itemBrowseSource.setEnabled(false); 153 } 154 155 public VisualizationDefinition getDefinition() { 156 return definition; 157 } 158 159 public void autoUpdateConfiguration(HashMap config) { 160 autoInterval = ((Integer)config.get("Interval")).intValue(); 161 PredictorFactory predictor = (PredictorFactory)config.get("Predictor"); 162 autoPredictorname = (predictor == null) ? null : predictor.getName(); 163 interval = autoInterval; 164 super.autoUpdateConfiguration(config); 165 } 166 167 public HashMap getCurrentConfigure() { 168 HashMap configure = super.getCurrentConfigure(); 169 170 configure.put("Interval", new Integer(interval)); 171 172 return configure; 173 } 174 175 public long getxMax() { 176 return xMax; 177 } 178 179 public AutoImage getImage() { 180 return image; 181 } 182 183 public void setImage(AutoImage image) { 184 this.image = image; 185 } 186 187 public void updateMenu() { 188 super.updateMenu(); 189 if (itemChangeOrientation == null) { 190 itemChangeOrientation = new JMenuItem("Change Orientation"); 191 itemChangeOrientation.setMnemonic(KeyEvent.VK_C); 192 itemChangeOrientation.addActionListener(new ActionListener() { 193 public void actionPerformed(ActionEvent e) { 194 changeOrientation(); 195 } 196 }); 197 popup.add(itemChangeOrientation); 198 } 199 } 200 201 public void disablePopupMenu() { 202 MenuElement[] menus = popup.getSubElements(); 203 for (int i=0; i<menus.length; i++) { 204 if (((JMenuItem)menus[i]).getText().equals("Sort") || 205 ((JMenuItem)menus[i]).getText().equals("Save...") || 206 ((JMenuItem)menus[i]).getText().equals("Restore") || 207 ((JMenuItem)menus[i]).getText().equals("Fit Window")) 208 continue; 209 ((JMenuItem)menus[i]).setEnabled(false); 210 } 211 Scene.getUIManager().disableFunctionMenus(); 212 } 213 214 protected void changeOrientation() { 215 AutoImage newImage = new AutoImage(); 216 int wMax = image.getW(); 217 int hMax = image.getH(); 218 Color color; 219 220 for (int i=0; i<wMax; i++) { 221 for (int j=0; j<hMax; j++) { 222 color = (Color)image.getColor(i,j); 223 if (color != null) { 224 newImage.setColor(j,i,color); 225 } 226 } 227 } 228 image = newImage; 229 230 normalOrientation = ! normalOrientation; 231 } 232 233 protected void switchThread(long threadId) { 234 if (currentThread != threadId) { 235 currentThread = threadId; 236 image = (AutoImage)imageMap.get(new Long(threadId)); 237 if (image == null) { 238 image = new AutoImage(); 239 imageMap.put(new Long(threadId),image); 240 } 241 } 242 } 243 244 protected void installPainter() { 245 painter = new DefaultPainter(); 246 } 247 248 protected void enableBrowseSourceMenu() { 249 itemBrowseSource.setEnabled(true); 250 } 251 252 protected void countEvents(long x) { 253 /*while (x/Math.abs(interval) >= eventCounter.size()) { 254 eventCounter.add(new Long(Scene.getEventCounter())); 255 } 256 257 while (x/Math.abs(interval) >= timeCounter.size()) { 258 timeCounter.add(new Long(x)); 259 }*/ 260 long eventNo = Scene.getEventCounter(); 261 long[] time2event = new long[2]; 262 time2event[0] = x; 263 time2event[1] = eventNo; 264 while (x/Math.abs(interval) - xOffset >= timeMap.size()) { 265 timeMap.add(time2event); 266 } 267 } 268 269 public void cleanup() { 270 super.cleanup(); 271 magnifier.cleanup(); 272 } 273 274 public void enablePhaseDetector(boolean enabled) { 275 phaseDetectorEnabled = enabled; 276 } 277 278 protected void reset() { 279 super.reset(); 280 image = null; 281 } 282 283 protected void dialogApply() { 284 super.dialogApply(); 285 image = null; 286 } 287 288 public PhaseDetector getPhaseDetector() { 289 return phaseDetector; 290 } 291 292 public Point getMousePosition() { 293 return new Point(mouseX, mouseY); 294 } 295 296 public void freeze(boolean flag) { 297 freezed = flag; 298 } 299 300 public Object clone() { 301 XYVisualization o = null; 302 303 o = (XYVisualization) super.clone(); 304 o.autoPredictorname = autoPredictorname; 305 o.image = (image == null) ? null : (AutoImage)image.clone(); 306 307 o.imageMap = HelperFuncs.cloneHashMap(imageMap); 308 o.itemChangeOrientation = null; 309 o.threadDataSet = HelperFuncs.cloneHashMap(threadDataSet); 310 o.sourceBrowser = (SourceBrowser)sourceBrowser.clone(); 311 o.painter = (painter == null) ?null : (Painter)painter.clone(); 312 o.itemBrowseSource = null; 313 o.itemChangeOrientation = null; 314 o.canvas = (AxesPanel)canvas.clone(); 315 o.panel = new JScrollPane(o.canvas); 316 o.canvas.setParent((JScrollPane)o.panel); 317 o.addMagnifier(o.canvas,flipMagnifier); 318 o.addPopupTrigger(o.panel); 319 o.timeMap = new ArrayList(); 320 for (int i=0; i<timeMap.size(); i++) { 321 long[] newValue = new long[2]; 322 long[] oldValue = (long[])timeMap.get(i); 323 newValue[0] = oldValue[0]; 324 newValue[1] = oldValue[1]; 325 o.timeMap.add(newValue); 326 } 327 return o; 328 } 329 330 public ArrayList getTimeMap() { 331 return timeMap; 332 } 333 334 public int getInterval() { 335 return interval; 336 } 337 338 protected abstract String getEntityUnderMouse(); 339 }